package com.piece.member.controller;

import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.log.config.UserContextHolder;
import com.piece.core.web.controller.FrameWorkController;
import com.piece.member.model.UmsMember;
import com.piece.member.service.UmsMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = {"会员管理"})
@RequestMapping("/api/member/manage")
public class UmsMemberController extends FrameWorkController {

    @Resource
    private UmsMemberService umsMemberService;

    @PostMapping("/list")
    @AclPermission(code = "system:member:view")
    @ApiOperation(value = "查询会员基础列表")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public List list() {
        QueryTable queryTable = initTable();
        DataTable dataTable = umsMemberService.findPage(queryTable);
        return dataTable.getData();
    }

    @PostMapping("/table")
    @AclPermission(code = "system:member:view")
    @ApiOperation(value = "查询会员基础分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse table() {
        QueryTable queryTable = initTable();
        Map<String, Object> params = queryTable.getParams();
        String keyVal = Convert.toStr(params.get("Q_key_S_LK"));
        if (StringUtil.isNotEmpty(keyVal)) {
            params.remove("Q_key_S_LK");
            params.put("Q_username_S_LK_OR", keyVal);
            params.put("Q_nickname_S_LK_OR", keyVal);
            params.put("Q_mobile_S_LK_OR", keyVal);
            params.put("Q_email_S_LK_OR", keyVal);
        }
        DataTable dataTable = umsMemberService.findPage(queryTable);
        return success(dataTable);
    }

    @GetMapping("/load")
    @AclPermission(code = "system:member:view")
    @ApiOperation(value = "加载会员基础表单数据")
    @ApiImplicitParam(name="id", value = "主键")
    public AjaxResponse load() {
        long id = Convert.toLong(getRequest().getParameter("id"), 0L);
        UmsMember umsMember = new UmsMember();
        if (id != 0) {
            umsMember = umsMemberService.findById(id);
        }
        return success(umsMember);
    }

    @SubmitPermission
    @PostMapping("/save")
    @AclPermission(code = "system:member:edit")
    @ApiOperation(value = "保存会员基础表单数据")
    @ApiImplicitParam(value = "会员基础数据", dataTypeClass = UmsMember.class, type = "body")
    public AjaxResponse save() {
        UmsMember umsMember;
        try {
            Long id = Convert.toLong(getRequest().getParameter("id"));
            if (null != id) {
                umsMember = umsMemberService.findById(id);
                umsMember.setSource(UserContextHolder.getUserId());
            } else {
                umsMember = new UmsMember();
                umsMember.setSource(UserContextHolder.getUserId());
                umsMember.setCreateTime(new Date());
            }
            initDB(umsMember);
            umsMemberService.insertOrUpdate(umsMember);
            return success(I18nUtil.message(ExceptionConstants.SAVE_SUCCESS), null);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SAVE_FAILURE), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/delete/{id}")
    @AclPermission(code = "system:member:delete")
    @ApiOperation(value = "删除会员基础数据")
    @ApiImplicitParam(name = "id", value = "主键", type = "path")
    public AjaxResponse delete(@PathVariable String id) throws Exception {
        umsMemberService.delete(id);
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/delete")
    @AclPermission(code = "system:member:delete")
    @ApiOperation(value = "删除会员基础数据")
    @ApiImplicitParam(name = "ids", value="主键集合")
    public AjaxResponse deleteBatch() throws Exception {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            umsMemberService.delete(Convert.toLongArray(ids));
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

}
